<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />

      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0"
        :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" size="small" label-width="80px">
         <!-- <el-form-item label="核销卡编号">
            <el-input v-model="form.id" style="width: 370px;" />
          </el-form-item> -->
          <el-form-item label="核销卡名称">
            <el-input v-model="form.name" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="数量">
            <el-input v-model="form.num" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="商品">
            <el-select v-model="form.productId" filterable clearable>
              <el-option v-for="item in productDataList" :disabled="item.disabled === 0" :value="item.id"
                :key="item.storeName" :label="item.storeName"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>

      <card ref="formd" />
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;"
        @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column v-if="columns.visible('id')" prop="id" label="核销卡编号" />
        <el-table-column v-if="columns.visible('name')" prop="name" label="核销卡名称" />
        <el-table-column v-if="columns.visible('num')" prop="num" label="数量" />
        <el-table-column v-if="columns.visible('createTime')" prop="createTime" label="添加时间">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column v-permission="['admin','yxStoreWriteCardMain:edit','yxStoreWriteCardMain:del']" label="操作"
          width="150px" align="center">
          <template slot-scope="scope">
              <el-button v-permission="['admin','yxStoreWriteCardMain:list']" size="mini" type="primary"
              @click="edit2(scope.row)">
              导出图片及数据
            </el-button>
           <!-- <el-button v-permission="['admin','YXSTORECOUPON_ALL','YXSTORECOUPON_EDIT']" size="mini" type="primary"
              @click="edit3(scope.row)">
              导出卡片数据
            </el-button> -->
          </template>

        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
  import {
    productList
  } from '@/api/yxStoreProduct'
  import crudYxStoreWriteCardMain from '@/api/yxStoreWriteCardMain'
  import CRUD, {
    presenter,
    header,
    form,
    crud
  } from '@crud/crud'
  import rrOperation from '@crud/RR.operation'
  import crudOperation from '@crud/CRUD.operation'
  import udOperation from '@crud/UD.operation'
  import pagination from '@crud/Pagination'
  import MaterialList from "@/components/material";
  import card from './card'
  import { parseTime, downloadFile } from '@/utils/index'
  // crud交由presenter持有
  const defaultCrud = CRUD({
    title: 'cardmain',
    url: 'api/yxStoreWriteCardMain',
    sort: 'id,desc',
    crudMethod: {
      ...crudYxStoreWriteCardMain
    }
  })
  const defaultForm = {
    id: null,
    name: null,
    productId: null,
    num: null,
    isDel: null,
    createTime: null,
    updateTime: null
  }
  export default {
    name: 'YxStoreWriteCardMain',
    components: {
      pagination,
      crudOperation,
      rrOperation,
      udOperation,
      MaterialList,
      productList,
      card
    },
    mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
    data() {
      return {

        permission: {
          add: [],
          edit: [],
          del: []
        },
        productDataList: []

      }
    },
    watch: {
    },
    methods: {
      // 获取数据前设置好接口地址
      [CRUD.HOOK.beforeRefresh]() {
        return true
      }, // 新增与编辑前做的操作
      [CRUD.HOOK.afterToCU](crud, form) {
        productList().then(res => {
          this.productDataList = res;
        })
      },
      editD(data) {
        console.log(data)
        const _this = this.$refs.formd
        _this.form = {
          id: data.id,
        }
        _this.dialog = true
        _this.spread(data.id)
      },
      edit2(data) {
        this.edit3(data);
        crud.downloadLoading = true
        crudYxStoreWriteCardMain.downloadZip(data.id).then(result => {
          downloadFile(result, data.name + '二维码', 'png')
          crud.downloadLoading = false
        }).catch(() => {
          crud.downloadLoading = false
        })

      },
      edit3(data) {
        crud.downloadLoading = true
        crudYxStoreWriteCardMain.downloadCard(data.id).then(result => {
          downloadFile(result, data.name + '数据', 'xlsx')
          crud.downloadLoading = false
        }).catch(() => {
          crud.downloadLoading = false
        })

      }
    }
  }
</script>

<style scoped>
  .table-img {
    display: inline-block;
    text-align: center;
    background: #ccc;
    color: #fff;
    white-space: nowrap;
    position: relative;
    overflow: hidden;
    vertical-align: middle;
    width: 32px;
    height: 32px;
    line-height: 32px;
  }
</style>
